<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelPages extends JModelList{
	
	protected $_context = 'com_surveys.pages';
	private $total=0;

	function populateState(){
		// Initialize variables.
		$app = JFactory::getApplication('administrator');
		// Load the list state.
		$this->setState('list.start', $app->getUserStateFromRequest($this->_context . '.list.start', 'limitstart', 0, 'int'));
		$this->setState('list.limit', $app->getUserStateFromRequest($this->_context . '.list.limit', 'limit', $app->getCfg('list_limit', 25) , 'int'));
		$this->setState('selected', JRequest::getVar('cid', array()));		
	}
	
	function getPagination(){
		$pagination=parent::getPagination();
		$pagination->total=$this->total;
		if($pagination->total%$pagination->limit>0){
			$nr_pages=intval($pagination->total/$pagination->limit)+1;
		}	
		else{ 
			$nr_pages=intval($pagination->total/$pagination->limit);
		}	
		$pagination->set('pages.total',$nr_pages);
		$pagination->set('pages.stop',$nr_pages);		
		
		return $pagination;
	}
	
	function getItems(){
		$config = new JConfig();	
		$app = JFactory::getApplication('administrator');
		$limistart = $app->getUserStateFromRequest($this->context.'.list.start', 'limitstart');
		$limit = $app->getUserStateFromRequest($this->context.'.list.limit', 'limit', $config->list_limit);

		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query = $this->getListQuery();
		
		$db->setQuery($query);
		$db->query();
		$result	= $db->loadObjectList();
		$this->total=count($result);
		
		$db->setQuery($query,$limistart,$limit);
		$db->query();
		$result	= $db->loadObjectList();
		
		return $result;
	}
	
	function getListQuery(){		
		$database	= JFactory::getDBO();
		$query		= $database->getQuery(true);
		$app 		= JFactory::getApplication('administrator');

		$filter_search = $app->getUserStateFromRequest($this->context.'.filter.search', 'search','','string');
		$this->setState('filter.search', $filter_search, 'string');
		
		$survey_filter = $app->getUserStateFromRequest($this->context.'.filter.survey', 'survey_filter','','string');
		$this->setState('filter.survey', $survey_filter, 'string');
		
		$where = " 1=1 ";
		
		if($survey_filter != "0" && $survey_filter != ""){
			$where .= " and p.survey_id=".intval($survey_filter);
		}
		
		if($filter_search != ""){
			$where .= " and p.title like '%".addslashes($filter_search)."%' ";
		}
		
		$s_id = JRequest::getVar("s_id", "", "get");
		if($s_id != ""){
			$where .= " and s.id=".$s_id;
		}
		
		$query->select('p.*, s.title as survey_name');
		$query->from('#__ijoomla_surveys_pages as p');
		$query->join('LEFT', '#__ijoomla_surveys_surveys AS s ON s.id = p.survey_id');	
		$query->where($where);
		$query->order("ordering asc");						
		return $query;		
	}
	
	function copyPage(){
		$ids = JRequest::getVar("cid");
		foreach($ids as $key=>$value){
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
			$query->clear();
			$query->select('*');
			$query->from('#__ijoomla_surveys_pages as p');	
			$query->where("id=".$value);
			$db->setQuery($query);
			$db->query();
			$row = $db->loadAssocList();
			
			$query->clear();
			$query->select('count(*)');
			$query->from('#__ijoomla_surveys_pages as p');	
			$query->where("title like '%".$row["0"]["title"]." Copy%'");
			$db->setQuery($query);
			$db->query();
			$countCopy = $db->loadResult();
			
			$countTotal = $this->getPagesList();
			
			$query->clear();
			$query->insert('#__ijoomla_surveys_pages');
			$query->set("survey_id=".$row["0"]["survey_id"]);
			$query->set("title='".$row["0"]["title"]." Copy ".($countCopy+1)."'");
			$query->set("description='".$row["0"]["description"]."'");
			$query->set("ordering=".($countTotal+1));
			$query->set("show_title=".$row["0"]["show_title"]);
			$query->set("published=".$row["0"]["published"]);
			$query->set("imagelist='".$row["0"]["imagelist"]."'");
			$db->setQuery($query);
			if(!$db->query()){
				return false;
			}
		}
		return true;
		 	 	 	 	 	 	 
	}
	
	function getPagesList(){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		$query->select("count(*)");
		$query->from("#__ijoomla_surveys_pages");
		$db->setQuery($query);
		$db->query();
		return $db->loadResult(); 
	}
	
	function getSurveysList(){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		$query->select("title, id");
		$query->from("#__ijoomla_surveys_surveys");
		$query->order("title asc");
		$db->setQuery($query);
		$db->query();
		return $db->loadAssocList(); 
	}
	
	function publish_unpublish(){
		$task = JRequest::getVar("task", "");
		$value = "";
		$ids = JRequest::getVar("cid");
		if($task == "publish"){
			$value = "1";
		}
		else{
			$value = "0";
		}
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->update("#__ijoomla_surveys_pages");
			$query->set("published=".$value);
			$query->where("id=".$id);
			$db->setQuery($query);
			if(!$db->query()){				
				return false;
			}
		}
		return true;
	}
	
	function remove(){
		$ids = JRequest::getVar("cid");
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->delete("#__ijoomla_surveys_pages");
			$query->where("id=".$id);
			$db->setQuery($query);
			if(!$db->query()){
				return false;
			}
		}
		return true;
	}
	
	function saveorder(){
		$ids = JRequest::getVar("cid");
		$orders = JRequest::getVar("order");
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->update("#__ijoomla_surveys_pages");
			$query->set("ordering=".$orders[$id]);
			$query->where("id=".$id);
			$db->setQuery($query);
			if(!$db->query()){				
				return false;
			}
		}
		return true;
	}
	
	function orderup(){
		$db =& JFactory::getDBO();
		$ids = JRequest::getVar("cid");
		$table =& $this->getTable();
		$table->load($ids["0"]);
		if(!$table->move(-1)){
			return false;
		}
		return true;
	}
	
	function orderdown(){
		$db =& JFactory::getDBO();
		$ids = JRequest::getVar("cid");
		$table =& $this->getTable();
		$table->load($ids["0"]);
		if(!$table->move(1)){
			return false;
		}
		return true;
	}
	
	function showtitle(){
		$task = JRequest::getVar("task", "");
		$value = "";
		$ids = JRequest::getVar("cid");
		if($task == "showtitle"){
			$value = "1";
		}
		else{
			$value = "0";
		}
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->update("#__ijoomla_surveys_pages");
			$query->set("show_title=".$value);
			$query->where("id=".$id);
			$db->setQuery($query);
			if(!$db->query()){				
				return false;
			}
		}
		return true;
	}
	
	function getQuestions($id){
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		$query->select("count(*)");
		$query->from("#__ijoomla_surveys_questions");
		$query->where("page_id=".$id);
		$db->setQuery($query);
		$db->query();
		return $db->loadResult();
	}
}

?>